import cv2
import numpy as np
from pyzbar import pyzbar
from PIL import Image, ImageEnhance

#sys.path.append('/home/ubuntu/.local/bin')

def qrsolver(img):
    cv2.imwrite("./TensorRT/Test/1.png", img)
    if isinstance(img, np.ndarray):
        img = Image.fromarray(np.array(img, np.uint8))
    barcodes = pyzbar.decode(img)

    scale = 7
    sharpen = 10.0
    contrast = 3.0
    brightness = 0.5

    if barcodes == []:
        img_scale = img
        for a in range(5):
            if barcodes != []:
                break
            img = img_scale
            img = img.resize((int(img.size[0] * scale), int(img.size[1] * scale)), Image.ANTIALIAS)
            img_sharpen = img

            sharpen = 8.0
            contrast = 1.0
            for b in range(6):
                if barcodes != []:
                    break
                img = img_sharpen
                img = ImageEnhance.Sharpness(img).enhance(sharpen)  # 锐利化
                img = ImageEnhance.Contrast(img).enhance(contrast)  # 增加对比度
                img_brightness = img

                brightness = 0.5
                for c in range(5):
                    if barcodes != []:
                        break
                    img = img_brightness
                    img = ImageEnhance.Brightness(img).enhance(brightness)  # 增加亮度
                    barcodes = pyzbar.decode(img)

                    brightness += 0.5
                contrast += 0.5
                sharpen += 1.0
            scale += 1
    print('[scale:{}, sharpen:{}, contrast:{}, brightness:{}]'.format(scale, sharpen, contrast, brightness))



    # scale = 7
    # sharpen = 8.0
    # contrast = 1.0
    # brightness = 0.5

    # if barcodes == []:
    #     img_scale = img
    #     for a in range(6):
    #         if barcodes != []:
    #             break
            # img = img_scale
            # img = img.resize((int(img.size[0] * scale), int(img.size[1] * scale)), Image.ANTIALIAS)
            # img_sharpen = img
            # for b in range(10):
            #     if barcodes != []:
            #         break
            #     img = img_sharpen
            #     img = ImageEnhance.Sharpness(img).enhance(sharpen)  # 锐利化
            #     img = ImageEnhance.Contrast(img).enhance(contrast)  # 增加对比度
            #     img_brightness = img
            #     for c in range(5):
            #         if barcodes != []:
            #             break
            #         img = img_brightness
            #         img = ImageEnhance.Brightness(img).enhance(brightness)  # 增加亮度
            #         barcodes = pyzbar.decode(img)
    
            #         brightness += 0.5
            #     contrast += 0.5
            #     sharpen += 1.0
            # scale += 1
            # print("qwqwqwqw")

    if barcodes == []:
        barcode = None
        label = None
    else:
        barcode = str(barcodes[0][0])
        barcode = barcode.split('\'')[1]
        label = barcode[-7:]
    return barcode, label

if __name__ == '__main__':
    img_path = '/media/Harddisk/Users/fan/PROJECT/CDPROJECT/TensorRT/TestData/qrimage/00.jpg'
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = Image.fromarray(np.array(img, dtype=np.uint8))
    barcode, label = qrsolver(img)
    print(barcode)
